﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace AMS.Common
{
    public class AccessWeb
    {

        public string AccessWebURL(string account, string password)
        {
            //DataTable dt = accountBll.GetAllAccountInfo();
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            string gethost = string.Empty;
            CookieContainer cc = new CookieContainer();
            string Cookiesstr = string.Empty;
            string ss = "";
            try
            {

                //第一次POST请求

                //string postdata = "__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKLTM0NjMxMjc3MQ9kFgICAw9kFgICAQ8PZBYCHglvbmtleWRvd24FG2NsZWFyRGVmYXVsdFRleHRCeUtleWRvd24oKWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFD0NoZWNrQm94U2F2ZVB3ZAULQnV0dG9uTG9naW64lXNq9UajkIsvdWwEvNpBXUrvng%3D%3D&__EVENTVALIDATION=%2FwEWBgLN6%2FrUDwLut%2B2uDgKpzpH0DQK12taIBQKWrKCPDwLK8b%2BXDcx1xvzlnmlKWtUhvCBbTJt8DBxL&TextBoxEmployeeNo={0}&TextBoxPassword={1}&ButtonLogin.x=112&ButtonLogin.y=17"; ;
                //string postdata = "LoginType=Explicit&user=t_runpuz&password=%24RFV5tgb";
                //string postdata = "__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&__VIEWSTATE=%2FwEPDwUKLTEyMjk0MDIzMg9kFgICAw9kFggCFQ8PFgIeB1Zpc2libGVoZGQCGw9kFgICAQ8QZGQWAGQCIQ8WAh4JaW5uZXJodG1sBVkyMDExLTA1LTE5IFVwZGF0ZSBJbmZv77yaQWdlbnQgZnVuY3Rpb24gaXMgb25saW5lLiBMb2dpbiBieSBkb21haW4gYWNjb3VudCBpcyBvbmxpbmUuIO%2B8m2QCIw8PFgIeBFRleHQFD1ZlcnNpb246My4wLjAuMGRkGAEFHl9fQ29udHJvbHNSZXF1aXJlUG9zdEJhY2tLZXlfXxYCBQljYkFkdmFuY2UFB2J0TG9naW76ieWaNXQXEgrKpniiao%2B8gVrbnw%3D%3D&__EVENTVALIDATION=%2FwEWCQLlp5mIAQKozOqLDQLapcrnAwLDs%2BvUDwLGvdL3AQLgr6WJAwK27rvNCwLHyfnnAgLgusDUCYvBuJOwzCcJv0Oxxt1c45cIgTtw&rblLoginType=UserID&tbUser=r11866&tbPWD=hisoft%2C123&btLogin.x=0&btLogin.y=0";
                string postdata = "destination=https%3A%2F%2Femail.hisoft.com%2Fowa%2F&flags=0&forcedownlevel=0&trusted=0&username=wuxi%5Cr11132&password=guxingbiao&isUtf8=1";
                postdata = string.Format(postdata, account, password.Replace("@", "%40"));
                //string LoginUrl = System.Configuration.ConfigurationManager.AppSettings["LoginURL"];
                //string LoginUrl = "https://access.citrite.net";
                string LoginUrl = "https://email.hisoft.com";
                request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
                request.Method = "POST";//数据提交方式为POST
                //模拟头
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabytes.Length;
                //request.Referer = "http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=" + HostUrl;
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                //提交请求
                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postdatabytes, 0, postdatabytes.Length);
                stream.Close();
                //接收响应
                response = (HttpWebResponse)request.GetResponse();
                //保存返回cookie
                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
                CookieCollection cook = response.Cookies;
                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
                Cookiesstr = strcrook;
                //取第一次GET跳转地址
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string content = sr.ReadToEnd();
                response.Close();
                string[] substr = content.Split(new char[] { '"' });

                //gethost = System.Configuration.ConfigurationManager.AppSettings["GetHost"];
                gethost = "https://email.hisoft.com/owa/";
            }
            catch (Exception)
            {
                //第一次POST出错；
            }
            try
            {
                request = (HttpWebRequest)WebRequest.Create(gethost);
                request.Method = "GET";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + Cookiesstr);
                request.CookieContainer = cc;
                request.AllowAutoRedirect = false;
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie
                Cookiesstr = request.CookieContainer.GetCookieHeader(request.RequestUri);
                //取再次跳转链接
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                ss = sr.ReadToEnd();
                string[] substr = ss.Split(new char[] { '"' });
                gethost = substr[1];
                request.Abort();
                sr.Close();
                response.Close();


            }
            catch (Exception)
            {
                //第一次GET出错
            }
            //try
            //{
            //    //推出系统
            //    //request = (HttpWebRequest)WebRequest.Create("");
            //    string postdataLogout = "__LASTFOCUS=&__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPDwUKLTM0NjMxMjc3MQ9kFgICAw9kFgICAQ8PZBYCHglvbmtleWRvd24FG2NsZWFyRGVmYXVsdFRleHRCeUtleWRvd24oKWQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFD0NoZWNrQm94U2F2ZVB3ZAULQnV0dG9uTG9naW64lXNq9UajkIsvdWwEvNpBXUrvng%3D%3D&__EVENTVALIDATION=%2FwEWBgLN6%2FrUDwLut%2B2uDgKpzpH0DQK12taIBQKWrKCPDwLK8b%2BXDcx1xvzlnmlKWtUhvCBbTJt8DBxL&TextBoxEmployeeNo={0}&TextBoxPassword={1}&ButtonLogin.x=112&ButtonLogin.y=17"; ;
            //    postdataLogout = string.Format(postdataLogout, account, password.Replace("@", "%40"));
            //    //string LoginUrl = System.Configuration.ConfigurationManager.AppSettings["LoginURL"];


            //    //request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
            //    request.Method = "POST";//数据提交方式为POST
            //    //模拟头
            //    request.ContentType = "application/x-www-form-urlencoded";
            //    byte[] postdatabytes = Encoding.UTF8.GetBytes(postdataLogout);
            //    request.ContentLength = postdatabytes.Length;
            //    //request.Referer = "http://www.renren.com/Login.do?rf=r&domain=renren.com&origURL=" + HostUrl;
            //    request.AllowAutoRedirect = false;
            //    request.CookieContainer = cc;
            //    request.KeepAlive = true;
            //    //提交请求
            //    Stream stream;
            //    stream = request.GetRequestStream();
            //    stream.Write(postdatabytes, 0, postdatabytes.Length);
            //    stream.Close();
            //    //接收响应
            //    response = (HttpWebResponse)request.GetResponse();
            //}

            return ss;
        }


    }
}
